package ccm.tools;

import ccm.common.CCMConfigManager;
import ccm.common.Utils;
import ccm.component.image.AdvancedResizeableImageJPanel;
import ccm.component.override.JFrame;
import ccm.component.override.JMenuItem;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

public class ImageViewer extends JFrame implements ActionListener, ChangeListener, KeyListener
{
    final protected AdvancedResizeableImageJPanel imageSourceJPanel;
    protected File[] fileList;
    protected volatile int fileListI=0;


    public ImageViewer()
    {
        super(new String[]{"tools","ImageViewer"});
        CCMConfigManager.setStringIfAbsent("ccm/tools/"+this.getClass().getName()+"/lastDir","");
        imageSourceJPanel=new AdvancedResizeableImageJPanel();
        add(imageSourceJPanel);

        addKeyListener(this);
        addKeyListener(imageSourceJPanel);
        imageSourceJPanel.addKeyListener(this);

        {
            JMenuItem jMenuItem1=new JMenuItem("重新选定路径");
            jMenuItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_DOWN_MASK|InputEvent.SHIFT_DOWN_MASK));
            setJMenu.add(jMenuItem1);
            jMenuItem1.addActionListener(e->selectFileDir());

            JMenuItem jMenuItem2=new JMenuItem("刷新");
            jMenuItem2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,0));
            setJMenu.add(jMenuItem2);
            jMenuItem2.addActionListener(e->updateFileDir());
        }
        setFocusable(true);
        setVisible(true);
        updateFileDir();
        //selectFileDir();
    }

    public static void main()
    {
        main(new String[0]);
    }

    public static void main(String[] args)
    {
        System.out.println(Arrays.toString(args));
        new ImageViewer();
    }

    protected void updateFileDir()
    {
        fileList=Utils.getFileList(CCMConfigManager.getString("ccm/tools/"+this.getClass().getName()+"/lastDir"));
        System.out.println(Arrays.toString(fileList));
        fileListI=0;
        updateImage();
        if(fileList.length==0)
        {
            selectFileDir();
        }
    }

    protected void selectFileDir()
    {
        try
        {
            JFileChooser fileChooser=new JFileChooser(CCMConfigManager.getString("ccm/tools/"+this.getClass().getName()+"/lastDir"));
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int returnVal=fileChooser.showOpenDialog(fileChooser);
            if(returnVal==JFileChooser.APPROVE_OPTION)
            {
                CCMConfigManager.setString("ccm/tools/"+this.getClass().getName()+"/lastDir",fileChooser.getSelectedFile().getAbsolutePath());
                updateFileDir();
            }
        }catch(Exception e)
        {
            e.printStackTrace();
            selectFileDir();
        }
    }

    public void actionPerformed(ActionEvent e)
    {

    }

    public void stateChanged(ChangeEvent e)
    {

    }

    public void keyTyped(KeyEvent e)
    {
    }

    synchronized public void keyPressed(KeyEvent e)
    {
        if(fileList==null)
        {
            System.out.println("null fileList");
            return;
        }
        if(e.getKeyCode()==KeyEvent.VK_A)
        {
            fileListI=fileListI-1;
        }
        else if(e.getKeyCode()==KeyEvent.VK_D)
        {
            fileListI=fileListI+1;
        }
        else if(e.getKeyCode()==KeyEvent.VK_F)
        {
            updateFileDir();
            updateImage();
        }
        if(fileListI<0) fileListI=fileList.length-1;
        if(fileListI>=fileList.length) fileListI=0;
        updateImage();
    }

    protected void updateImage()
    {
        try
        {
            System.out.println(fileList[fileListI].toPath().getFileName().toString());
            imageSourceJPanel.setImage(ImageIO.read(fileList[fileListI]));
            setTitle(fileList[fileListI].getName());
        }catch(IOException|ArrayIndexOutOfBoundsException ignored)
        {
            JOptionPane.showMessageDialog(this,fileList[fileListI].getName(),"图片打开错误",JOptionPane.ERROR_MESSAGE);
        }
    }

    public void keyReleased(KeyEvent e)
    {

    }

    @Override
    public void restart()
    {
        dispose();
        main();
    }
}
